<?php

/**
 * Configuration class
 * Singleton launch with getInstance()
 *
 * @todo Config file path to GLOBAL config
 * @todo save ini
 *
 * @property private
 * @property private static instance
 *
 * @example  $c = config::getInstance();
             $c->get("sitetitle");
 */

final class FWconfig {

    const CONFIG_INI_FILE = 'config/config.ini';
    
    private $configuration = array();
    private static $instance = null;
    private function __construct() { }
    private function __clone() { }

    /**
     * Load Config singleton
     * @return config instance
     */
    public static function getInstance() {
        if(self::$instance === null) {
            self::$instance = new config();
            self::$instance->readINI();
        }
        return self::$instance;
    }
    
    /**
     * load up the CONFIG_INI_FILE
     */
    public function readINI() {
        if(file_exists( self::CONFIG_INI_FILE )) {
            $ini = parse_ini_file( self::CONFIG_INI_FILE );
            foreach($ini as $key => $value) {
                $this->set($key, $value);
            }
        }
        else {
            throw new Exception("Die ini-Datei existiert nicht!");
        }
    }

    /**
     * Set a Config key
     * @param string key
     * @param value
     */
    public function set($key, $value) {
        $this->configuration[strtolower($key)] = $value;
    }

    /**
     * Returns a configuration
     * @param key
     * @return value
     */
    public function get($key) {
        if($this->exists($key)) {
            return $this->configuration[strtolower($key)];
        }
        else {
            return false;
        }
    }

    /**
     * checks if a config key exists
     * @param key
     * @return bool
     */
    public function exists($key) {
        if(isset($this->configuration[strtolower($key)])) {
            return true;
        }
        else {
            return false;
        }
    }
}
?>